﻿#include <sb.h>

#define MAXCOUNT	50000

int qsort_callback(kconstpointer p1, kconstpointer p2)
{
	kint l = *(kint*)p1;
	kint r = *(kint*)p2;

	if (l > r)
		return 1;
	else if (l < r)
		return -1;
	return 0;
}

int main()
{
	k_mesg("TPL SORT TEST\n\n");

	k_srand(0);

	tpl::VecArray<int> vcq(MAXCOUNT);

	for (int i = 0; i < MAXCOUNT; i++)
		vcq.Add(k_rand_int(0, 1000000));

	tpl::VecArray<int> vth(vcq.Begin(), vcq.end());
	tpl::VecArray<int> vtq(vcq.Begin(), vcq.end());

	double ds, dcq, dth, dtq;

	// k_qsort
	ds = k_curtick();
	k_qsort(vcq.Data(), vcq.Count(), sizeof(int), qsort_callback);
	dcq = k_curtick() - ds;

	//for (int i = 0; i < 10; i++)
	//for (int i = MAXCOUNT - 1; i >= MAXCOUNT - 10; --i)

	for (int i = MAXCOUNT - 1; i >= MAXCOUNT - 10; --i)
		k_mesgf("[VCQ#%d] %d\n", i, vcq[i]);
	k_mesgf("ELAPSED: %.5f\n\n", dcq);

	// heapsort
	ds = k_curtick();
	tpl::HeapSort(vth.Data(), vth.Count());
	dth = k_curtick() - ds;

	for (int i = MAXCOUNT - 1; i >= MAXCOUNT - 10; --i)
		k_mesgf("[VTH#%d] %d\n", i, vth[i]);
	k_mesgf("ELAPSED: %.5f\n\n", dth);

	// quicksort
	ds = k_curtick();
	tpl::QuickSort(vtq.Data(), vtq.Count());
	dtq = k_curtick() - ds;

	for (int i = MAXCOUNT - 1; i >= MAXCOUNT - 10; --i)
		k_mesgf("[VTQ#%d] %d\n", i, vtq[i]);
	k_mesgf("ELAPSED: %.5f\n\n", dtq);

	return 0;
}
